package com.wanyang.exception;

import com.wanyang.vo.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ApiResult handle(Exception e) {
        log.error(e.getMessage(), e);
        Integer code;
        if (e instanceof ApiException) {
            code = ((ApiException) e).getCode();
        } else {
            code = ApiResult.INNER_ERROR;
        }
        return ApiResult.error(code, null, e.getMessage());
    }

}
